import request from '@/utils/request'
import { LOGIN_SETTOKEN, LOGOUT, DEL_USERINFO } from '../actionsTypes'
import { removeToken, setToken } from '@/utils/storage'
export const setLoginToken = ({ code, mobile }) => {
  return async (dispatch) => {
    const res = await request({
      url: '/authorizations',
      method: 'POST',
      data: {
        code,
        mobile
      }
    })
    console.log(res)
    setToken(res.data.token)
    dispatch({ type: LOGIN_SETTOKEN, payload: res.data.token })
  }
}

export const logout = () => {
  return (dispatch) => {
    // 清除token
    dispatch({ type: LOGOUT })
    removeToken()
    // 清除用户信息
    dispatch({ type: DEL_USERINFO })
  }
}
